package controllers

import config.{Global, MyFile}
import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}

import scala.concurrent.ExecutionContext

class UtilsController@Inject()(cc: ControllerComponents)
                              (implicit exec: ExecutionContext) extends AbstractController(cc) with MyFile {


  def downloadExampleFile  = Action { implicit request =>
    Ok.sendFile(s"${Global.path}/example/example.fa".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> s"attachment; filename=example.fa",
      CONTENT_TYPE -> "application/x-download"
    )
  }

  def downloadFile(file:String) = Action{implicit request=>
    val name = file.replaceAll("\\\\","/").split("/").last
    Ok.sendFile(s"${Global.path}/download/$name".toFile).withHeaders(
      //缓存
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> s"attachment; filename=$name",
      CONTENT_TYPE -> "application/x-download"
    )

  }
}
